<!-- 确认入库 -->
<template>
  <view class="confirmWarehousing">
    <view class="top">
      <van-cell-group>
        <van-field
          :value="form.purchaseinfo.applyname"
          label="采购姓名："
          readonly
        ></van-field>
        <van-field
          :value="form.purchaseinfo.purchase_reason"
          label="采购事由:"
          readonly
        ></van-field>
      </van-cell-group>
    </view>
    <!-- 套餐类型 -->
    <view class="content" v-if="order_type == '0'">
      <view class="title">
        <view class="title_left">商品明细</view>
        <view class="title_right">
          <view
            >合计：<text
              >￥{{
                form.purchasegoodsinfo.reduce(
                  (prev, item) => prev * 1 + item.total_money * 1,
                  0
                )| number_format(2)
              }}</text
            ></view
          >
          <view class="materials" @click="materials">包含材料</view>
        </view>
      </view>
      <view class="list_box">
        <view
          class="list_item"
          v-for="(item, index) in form.purchasegoodsinfo"
          :key="index"
        >
          <view class="list_item_title">
            <view class="name">
              <view class="point"></view>
              <view>时间：{{ item.ctime }}</view>
            </view>
            <view class="quantity">
              <text>类型：</text>
              {{ item.ctype }}
            </view>
          </view>
          <view> 菜名： {{ item.foods_name_str }} </view>
          <view class="time_box">
            <view> 老师数量：{{ item.goods_num }} </view>
            <view> 学生数量：{{ item.goods_num_stu }} </view>
          </view>
          <view class="locations_unitrice-box">
            <view class="unit_price">
              单价 ：<text>￥ {{ item.price }}</text>
            </view>
          </view>
          <view class="details_subtotal-box">
            <view class="details" @click="details(item)">详情 </view>
            <view class="subtotal">
              小计:<text>￥{{ item.total_money }}</text></view
            >
          </view>
        </view>
      </view>
    </view>
    <view class="content" v-else>
      <view class="title">
        <view class="title_left">商品明细</view>
        <view class="title_right">
          <view
            >共 <text>{{ form.purchasegoodsinfo.length }}</text
            >件商品
          </view>
          <view
            >合计：<text
              >￥{{
                form.purchasegoodsinfo.reduce(
                  (prev, item) => prev * 1 + item.total_money * 1,
                  0
                )| number_format(2)
              }}</text
            ></view
          >
        </view>
      </view>
      <view class="list_box">
        <view
          class="list_item"
          v-for="(item, index) in form.purchasegoodsinfo"
          :key="index"
        >
          <view class="list_item_title">
            <view class="name">
              <view class="point"></view>
              <view>品名：{{ item.goods_name }}</view>
            </view>
            <view class="quantity">
              <text>数量：</text>
              {{ item.goods_num }}
              <text>{{ item.unit_name }}</text>
            </view>
          </view>
          <view> 供应商： {{ item.supplier_name }} </view>
          <view class="time_box">
            <view> 生产日期：{{ item.productiontime }} </view>
            <view> 保质期（天）：{{ item.assurancetime }} </view>
          </view>
          <view class="locations_unitrice-box">
            <view> 产地：{{ item.address }} </view>
            <view class="unit_price">
              单价 ：<text>￥ {{ item.goods_price }}</text>
            </view>
          </view>
          <view class="details_subtotal-box">
            <view class="details" @click="details(item)">详情 </view>
            <view class="subtotal">
              小计:<text>￥{{ item.total_money }}</text></view
            >
          </view>
        </view>
      </view>
    </view>
    <view class="btn-box">
      <van-button color="#999999 " type="info" @click="disagree"
        >不同意</van-button
      >
      <van-button color="#62B158" type="info" @click="okProject"
        >同意</van-button
      >
    </view>

    <!-- 详情弹框 -->
    <van-overlay :show="detailsShow" z-index="99">
      <view class="wrapper">
        <view class="block">
          <view class="title">
            <view> 详情 </view>
            <view class="icon" @click="offShow">
              <van-icon name="clear" color="#3E3D3D" />
            </view>
          </view>
          <van-cell-group v-if="order_type == '0'">
            <van-field
              :value="detailForm.ctime"
              label="时间："
              readonly
            ></van-field>
            <van-field
              :value="detailForm.ctype"
              label="类型："
              readonly
            ></van-field>
            <van-field
              :value="detailForm.foods_name_str"
              label="菜名："
              readonly
            ></van-field>
            <!-- <van-field
              :value="detailForm.supplier_name"
              label="供应商"
              readonly
            ></van-field> -->
            <view class="supplier" >
              <view class="supplier-laber">
                供应商
              </view>
                <view class="supplier-name">
                  {{ detailForm.supplier_name }}
                </view>
            </view>
              
            <van-field
              :value="detailForm.price"
              label="单价："
              readonly
            ></van-field>
            <van-field
              :value="detailForm.goods_num_stu"
              label="学生数量："
              readonly
            ></van-field>
            <van-field
              :value="detailForm.goods_num"
              label="老师数量："
              readonly
            ></van-field>
            <van-field
              :value="detailForm.unit_name"
              label="单位："
              readonly
            ></van-field>
            <van-field
              :value="detailForm.total_money"
              label="小计："
              readonly
            ></van-field>
          </van-cell-group>
          <van-cell-group v-else>
            <van-field
              :value="detailForm.goods_name"
              label="商品："
              readonly
            ></van-field>
            <!-- <van-field
              :value="detailForm.supplier_name"
              label="供应商："
              readonly
            >
          </van-field> -->
            <view class="supplier" >
              <view class="supplier-laber">
                供应商
              </view>
                <view class="supplier-name">
                  {{ detailForm.supplier_name }}
                </view>
            </view>
              
            <van-field
              :value="detailForm.unit_name"
              label="单位："
              readonly
            ></van-field>
            <van-field
              :value="detailForm.goods_price"
              label="单价"
              readonly
            ></van-field>
            <van-field
              :value="detailForm.goods_num"
              label="数量："
              readonly
            ></van-field>
            <van-field
              :value="detailForm.address"
              label="产地："
              readonly
            ></van-field>
            <van-field
              :value="detailForm.is_nutrition"
              label="是否营养餐："
              readonly
            ></van-field>
            <van-field
              :value="detailForm.assurancetime"
              label="保质期（天）："
              readonly
            ></van-field>
            <van-field
              :value="detailForm.productiontime"
              label="生产日期："
              readonly
            ></van-field>
            <view class="upload">
              <van-field
                :value="detailForm.filesrc"
                name="filesrc"
                data-name="filesrc"
                readonly
                label="检测报告："
              >
                <view slot="input">
                  <van-uploader
                    :deletable="false"
                    :fileList="fileList"
                    @after-read="afterRead"
                    max-count="1"
                  />
                </view>
              </van-field>
            </view>

            <van-field
              :value="detailForm.total_money"
              label="小计："
              readonly
            ></van-field>
          </van-cell-group>
        </view>
      </view>
    </van-overlay>

    <!-- 不同意弹框 -->
    <van-overlay :show="disagreeShow">
      <view class="wrapper">
        <view class="block">
          <view class="title">
            <view> 不同意 </view>
            <view class="icon" @click="offShow">
              <van-icon name="clear" color="#3E3D3D" />
            </view>
          </view>
          <view class="content">
            <view class="title"> 审核人意见: </view>
            <view>
              <van-cell-group rows="5">
                <van-input
                  v-model="opinion"
                  rows="8"
                  type="textarea"
                  placeholder="请填写意见"
                  autosize
                  border
                  :norequired="true"
                ></van-input>
              </van-cell-group>
            </view>
            <view class="btn_style">
              <van-button color="#999999 " type="info" @click="disagree"
                >取消</van-button
              >
              <van-button color="#62B158" type="info" @click="sumbitProject"
                >提交</van-button
              >
            </view>
          </view>
        </view>
      </view>
    </van-overlay>
  </view>
</template>
  
<script>
export default {
  data() {
    return {
      fileList: [],
      purchase_sn_detail: "", //采购单编号
      order_type: "", //类型
      confirm: "", //是否同意出库
      form: {
        purchaseinfo: {},
        purchasegoodsinfo: [],
      },
      detailsShow: false,
      detailForm: {},
      disagreeShow: false,
      opinion: "", //意见
      autosize: {
        maxHeight: "100px",
        minHeight: 50,
      },
    };
  },

  // 监听页面加载，该钩子被调用时
  onLoad(options) {
    this.purchase_sn_detail = options.purchase_sn_detail;
    if (this.purchase_sn_detail) {
      this.getPurchaseokdetail();
    }
  },
  methods: {
    /*提交（不同意）*/
    async sumbitProject() {
        this.confirm = "0";
        if (this.confirm == "0") {
          let res = await this.$ajax.onpurchaseok({
            purchase_sn_detail: this.purchase_sn_detail,
            confirm: this.confirm,
            opinion: this.opinion,
          });
          if (res.code == 1) {
            uni.showToast({
              title: res.msg,
              icon: "none",
              duration: 1000,
            });
            this.disagreeShow = false;
            uni.navigateTo({
              url:"../purchaseOrder/purchaseOrder"
            })

          } else {
            uni.showToast({
              title: res.msg,
              icon: "none",
              duration: 1000,
            });
          }
        }

    },
    /*同意*/
    async okProject() {
      this.confirm = 1;
      if (this.confirm == 1) {
        let res = await this.$ajax.onpurchaseok({
          purchase_sn_detail: this.purchase_sn_detail,
          confirm: this.confirm,
        });
        if (res.code == 1) {
          uni.showToast({
            title: res.msg,
            icon: "none",
          });
          setTimeout(() => {
            uni.navigateTo({
              url:
                "../purchaseOrder/warehouseWarrant?purchase_sn_detail=" +
                this.purchase_sn_detail,
            });
          }, 1000);
        } else {
          uni.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      }
    },
    /*包含材料*/
    materials() {
      uni.navigateTo({
        url:
          "../purchaseOrder/materials?purchase_sn_detail=" +
          this.purchase_sn_detail,
      });
    },
    /*文件上传*/
    afterRead(event) {
      const { file } = event.detail;
      // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
      wx.uploadFile({
        url: this.API + "/api/purchasemanage/upload", // 仅为示例，非真实的接口地址
        filePath: file.url,
        name: "file",
        formData: { file: "file" },
        header: {
          token: uni.getStorageSync("token"),
        },
        success: (res) => {
          console.log(JSON.parse(res.data).data);
          this.fileList.push({
            ...file,
            url: JSON.parse(res.data).data.fullurl,
            isImage: true,
            // deletable: true
          });
          this.$set(this.addForm, "filesrc", JSON.parse(res.data).data.url); // 上传相对地址或者 绝对地址 ，后期可能需要改
        },
      });
    },
    startUploadFile() {
      wx.chooseImage({
        count: 1,
        sizeType: ["original", "compressed"],
        sourceType: ["album", "camera"],
        success: (res) => {
          console.log(res);
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths[0];
          wx.uploadFile({
            url: this.API + "/api/purchasemanage/upload", //仅为示例，非真实的接口地址
            filePath: tempFilePaths,
            name: "file",
            formData: {
              user: "test",
            },
            success: (res) => {
              console.log(res);
              //do something
            },
          });
        },
      });
    },
    /*获取页面数据*/
    async getPurchaseokdetail() {
      if (this.purchase_sn_detail) {
        let res = await this.$ajax.onPurchaseokdetail({
          purchase_sn_detail: this.purchase_sn_detail,
        });
        if (res.code == 1) {
          this.order_type = res.data.purchaseinfo.order_type;
          this.form.purchaseinfo = res.data.purchaseinfo;
          if (this.order_type == "0") {
            // 套餐
            this.form.purchasegoodsinfo = res.data.purchase_menu;
          } else {
            this.form.purchasegoodsinfo = res.data.purchasegoodsinfo;
          }
        } else {
          uni.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      }
    },
    /*详情*/
    details(item) {
      this.detailsShow = true;
      this.detailForm = item;
      if (this.detailForm.is_nutrition == "0") {
        this.detailForm.is_nutrition = "否";
      } else {
        this.detailForm.is_nutrition = "是";
      }
      if (this.detailForm.filesrc) {
        this.fileList.push({
          url: this.API + this.detailForm.filesrc,
          isImage: true,
        });
      }
    },

    // 关闭
    offShow() {
      this.detailsShow = false;
      this.disagreeShow = false;
    },
    /*不同意*/
    disagree() {
      this.disagreeShow = true;
    },
  },

  mounted() {},

  // 监听页面显示，页面每次出现在屏幕上都触发
  onShow() {
    uni.setNavigationBarTitle({
      title: "确认入库",
    });
  },

  // 监听用户下拉动作，一般用于下拉刷新
  onPullDownRefresh() {},

  // 监听页面初次渲染完成，此时组件已挂载完成
  onReady() {},
};
</script>
  
<style lang="scss">
.supplier{
  display: flex;
   padding-left: 5%;
  .supplier-laber {
  width: 37%;
  display: flex;
  align-items: center;
}
.supplier-name {
  display: flex;
  flex-wrap: wrap;
}
}
.materials {
  display: flex;
  justify-content: space-between;
  color: rgba(153, 153, 153, 1);
  font-size: 26rpx;
  font-family: SourceHanSansSC-regular;
  font-size: 24rpx;
  border: 1px solid;
  color: white;
  background: #2e88ee;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0rpx 10rpx;
  margin-left: 10rpx;
}
/*检测报告*/
.upload {

  .van-field__body {
    height: 180rpx;
  }
}

.confirmWarehousing {
  width: 100%;
  height: 100vh;
  background: rgba(244, 244, 244, 1);

  .top {
    .van-field__label {
      color: rgba(16, 16, 16, 1);
      font-size: 28rpx;
      font-family: SourceHanSansSC-regular;
    }
  }

  .content {
    .title {
      display: flex;
      justify-content: space-between;
      padding: 20rpx 30rpx;

      .title_left {
        color: rgba(153, 153, 153, 1);
        font-size: 26rpx;
        font-family: SourceHanSansSC-regular;
      }

      .title_right {
        display: flex;
        justify-content: space-between;
        color: rgba(153, 153, 153, 1);
        font-size: 26rpx;
        font-family: SourceHanSansSC-regular;

        text {
          color: red;
          font-weight: bold;
        }
      }
    }

    .list_box {
      .list_item {
        width: 100%;
        line-height: 55rpx;
        background-color: rgba(255, 255, 255, 1);
        color: rgba(16, 16, 16, 1);
        font-size: 28rpx;
        padding: 20rpx 20rpx;
        font-family: SourceHanSansSC-regular;
        margin-bottom: 20rpx;

        .list_item_title {
          display: flex;
          justify-content: space-between;

          .name {
            display: flex;
            color: rgba(16, 16, 16, 1);
            font-size: 28rpx;
            font-family: SourceHanSansSC-bold;
            align-items: center;
            font-weight: bold;

            .point {
              width: 15rpx;
              height: 15rpx;
              border-radius: 50%;
              background: rgba(98, 177, 88, 1);
              margin-right: 10rpx;
            }
          }
        }

        .time_box {
          display: flex;
          justify-content: space-between;
        }

        .locations_unitrice-box {
          display: flex;
          justify-content: space-between;
          border-bottom: 0.5rpx solid #e7e4e4;
          padding-bottom: 20rpx;

          .unit_price {
            text {
              color: red;
            }
          }
        }

        .details_subtotal-box {
          display: flex;
          justify-content: space-between;
          padding-top: 20rpx;

          .details {
            color: rgba(46, 136, 238, 1);
            font-size: 28rpx;
            font-family: SourceHanSansSC-regular;
          }

          .subtotal {
            background-color: rgba(255, 255, 255, 1);
            color: rgba(209, 35, 18, 1);
            font-size: 28rpx;
            text-align: center;
            font-family: Arial;
            font-weight: bold;
          }
        }
      }
    }
  }
  .btn-box {
    display: flex;
    justify-content: space-evenly;
    margin-top: 50rpx;
    padding-bottom: 100rpx;

    .van-button {
      width: 180rpx;
      height: 72rpx;
      border-radius: 8rpx;
      color: rgba(255, 255, 255, 1);
      font-size: 28rpx;
      text-align: center;
      font-family: Microsoft Yahei;
    }
  }
}

// 弹框样式
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;

  .block {
    width: 100%;
    height: auto !important;
    background-color: #fff;
    margin: 0 30rpx;
    border-radius: 16px;
    padding: 40rpx 20rpx;
    font-size: 28rpx;

    .title {
      display: flex;
      justify-content: space-between;
      color: rgba(16, 16, 16, 1);
      font-size: 32rpx;
      font-family: SourceHanSansSC-bold;
    }

    .van-field__label {
      width: 200rpx;
    }

    // 不同意
    .content {
      margin-bottom: 68rpx;

      .van-cell-group {
      }

      .btn_style {
        display: flex;
        justify-content: space-evenly;
        margin-top: 66rpx;
        

        .van-button {
          width: 180rpx;
          height: 72rpx;
          border-radius: 8rpx;
          color: rgba(255, 255, 255, 1);
          font-size: 28rpx;
          text-align: center;
          font-family: Microsoft Yahei;
        }
      }
    }
  }
}
</style>